home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / computerjanitor / plugin_tests.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  3.8 KB  |  113 lines

  1. # plugin_tests.py - unit tests for plugin.py
  2. # Copyright (C) 2008  Canonical, Ltd.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, version 3 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15.  
  16.  
  17. import os
  18. import tempfile
  19. import unittest
  20.  
  21. import computerjanitor
  22.  
  23.  
  24. class PluginTests(unittest.TestCase):
  25.  
  26.     def testGetCruftRaisesException(self):
  27.         p = computerjanitor.Plugin()
  28.         self.assertRaises(computerjanitor.UnimplementedMethod, p.get_cruft)
  29.  
  30.     def testPostCleanupReturnsNone(self):
  31.         p = computerjanitor.Plugin()
  32.         self.assertEqual(p.post_cleanup(), None)
  33.  
  34.     def testDoesNotHaveAppAttributeByDefault(self):
  35.         p = computerjanitor.Plugin()
  36.         self.assertFalse(hasattr(p, "app"))
  37.  
  38.     def testSetApplicationSetsApp(self):
  39.         p = computerjanitor.Plugin()
  40.         p.set_application("foo")
  41.         self.assertEqual(p.app, "foo")
  42.  
  43.     def testSetsRequiredConditionToNoneByDefault(self):
  44.         p = computerjanitor.Plugin()
  45.         self.assertEqual(p.condition, [])
  46.  
  47.  
  48. class PluginManagerTests(unittest.TestCase):
  49.  
  50.     def testFindsNoPluginsInEmptyDirectory(self):
  51.         tempdir = tempfile.mkdtemp()
  52.         pm = computerjanitor.PluginManager(None, [tempdir])
  53.         plugins = pm.get_plugins()
  54.         os.rmdir(tempdir)
  55.         self.assertEqual(plugins, [])
  56.  
  57.     def testFindsOnePluginFileInTestPluginDirectory(self):
  58.         pm = computerjanitor.PluginManager(None, ["testplugins"])
  59.         self.assertEqual(pm.get_plugin_files(), 
  60.                          ["testplugins/hello_plugin.py"])
  61.  
  62.     def testFindsOnePluginInTestPluginDirectory(self):
  63.         pm = computerjanitor.PluginManager(None, ["testplugins"])
  64.         self.assertEqual(len(pm.get_plugins()), 1)
  65.  
  66.     def testFindPluginsSetsApplicationInPluginsFound(self):
  67.         pm = computerjanitor.PluginManager("foo", ["testplugins"])
  68.         self.assertEqual(pm.get_plugins()[0].app, "foo")
  69.  
  70.     def callback(self, filename, index, count):
  71.         self.callback_called = True
  72.  
  73.     def testCallsCallbackWhenFindingPlugins(self):
  74.         pm = computerjanitor.PluginManager(None, ["testplugins"])
  75.         self.callback_called = False
  76.         pm.get_plugins(callback=self.callback)
  77.         self.assert_(self.callback_called)
  78.  
  79.  
  80. class ConditionTests(unittest.TestCase):
  81.  
  82.     def setUp(self):
  83.         self.pm = computerjanitor.PluginManager(None, ["testplugins"])
  84.  
  85.         class White(computerjanitor.Plugin):
  86.             pass
  87.  
  88.         class Red(computerjanitor.Plugin):
  89.             def __init__(self):
  90.                 self.condition = ["red"]
  91.  
  92.         class RedBlack(computerjanitor.Plugin):
  93.             def __init__(self):
  94.                 self.condition = ["red","black"]
  95.  
  96.         self.white = White()
  97.         self.red = Red()
  98.         self.redblack = RedBlack()
  99.         self.pm._plugins = [self.white, self.red, self.redblack]
  100.  
  101.     def testReturnsOnlyConditionlessPluginByDefault(self):
  102.         self.assertEqual(self.pm.get_plugins(), [self.white])
  103.  
  104.     def testReturnsOnlyRedPluginWhenConditionIsRed(self):
  105.         self.assertEqual(self.pm.get_plugins(condition="red"), [self.red, self.redblack])
  106.  
  107.     def testReturnsOnlyRedPluginWhenConditionIsRedAndBlack(self):
  108.         self.assertEqual(self.pm.get_plugins(condition=["red","black"]), [self.redblack])
  109.  
  110.     def testReturnsEallPluginsWhenRequested(self):
  111.         self.assertEqual(set(self.pm.get_plugins(condition="*")),
  112.                          set([self.white, self.red, self.redblack]))
  113.